// json数组转换成图形对象
function transWeapon (data) {
  data.forEach((item, i) => {
    turnSelf(item)
    changeSlib(data, i)
  })
}

function turnSelf (data) {
  data.nodes = []
  if (data.level === 1) {
    data.nodes.push('D')
  } else {
    for (let i = 2; i < data.level; i++) {
      data.nodes.push(' ')
    }
    data.nodes.push('L')
    data.nodes.push('D')
  }
}

/**
 * 0:表示空格 1：表示转折 2.表示数据 3.表示连线
 */
function changeSlib (data, i) {
  let item = data[i]
  // 查找父节点或兄弟节点索引
  if (item.level !== 1 && data[i - 1].level !== (item.level - 1)) { // 非第一个子节点
    let siblingIndex = 0
    // 查找兄弟结点
    for (let k = 1; k < i; k++) {
      if (data[k].level === data[i].level) {
        siblingIndex = k
      }
    }
    // 十字线
    data[siblingIndex].nodes[item.level - 2] = '+'
    // 兄弟节点间补充
    for (siblingIndex = siblingIndex + 1; siblingIndex < i; siblingIndex++) {
      data[siblingIndex].nodes[item.level - 2] = '|'
    }
  }
}
let data = [
  {level: 1, name: 'A'}, {level: 2, name: 'B'}, {level: 2, name: 'C'}, {level: 3, name: 'D'}, {level: 4, name: 'E'},
  {level: 2, name: 'F'}, {level: 3, name: 'G'}, {level: 4, name: 'H'}, {level: 3, name: 'O'}
]
transWeapon(data)
console.log(data)
